/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fwdisp.gui.tabs.settings.ausrueckPanelElements;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import settings.MainSettingsManager;
import settings.collections.ETypeCollection;
import settings.collections.elements.AusrueckOrdnung;
import settings.collections.elements.EType;

/**
 *
 * @author rbs
 */
public class AusrueckAddFrame extends JFrame implements KeyListener{

    private static AusrueckOrdnung aro;
    private final JTextField nameField = new JTextField();
    private final JComboBox etypes = new JComboBox();
    private final JTextField containingStrings = new JTextField();
    private final JTextField unleashStrings = new JTextField();
    private final JCheckBox autoName = new JCheckBox("automatisch erzeugen");
    
    
    public AusrueckAddFrame(AusrueckOrdnung ordnung) {
        loadETypes();
        nameField.setText(ordnung.getName());
        etypes.setSelectedIndex(ordnung.getEType().getId());
        String[] containing = ordnung.getContainingStrings();
        String conString = "";
        for (String elem: containing)
            if (conString.equals(""))
                conString += elem;
            else
                conString += "," + elem;
        
        containingStrings.setText(conString);
        
        
        String[] unleashed = ordnung.getUnleashStrings();
        String unlString = "";
        for (String elem: unleashed)
            if (unlString.equals(""))
                unlString += elem;
            else
                unlString += "," + elem;
        
        unleashStrings.setText(unlString);
        create(true);
    }
    
    public AusrueckAddFrame() {
        loadETypes();
        create(false);
    }
    
    private void loadETypes()
    {
        ArrayList<EType> eTypes = MainSettingsManager.getSetting().geteTypeCollection().getETypes();
        for (EType type : eTypes)
        {
            etypes.addItem(type.getId() + " - " + type.getName());
        }
        
        etypes.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                setAutoName();
            }
        });
    }
    
    private void create(boolean change)
    {
        setMinimumSize(new Dimension(600, 400));
        
        JPanel contentPanel = new JPanel(new GridLayout(10, 1, 20, 10));
        contentPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        JLabel nameLabel = new JLabel("Name:");
       
        autoName.setSelected(true);
        autoName.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                if (autoName.isSelected())
                    nameField.setEnabled(false);
                else
                    nameField.setEnabled(true);
            }
        });
        nameLabel.setEnabled(false);
        
        JLabel etypes_label = new JLabel("Einsatztyp");
        
        
        JLabel containingStringsLabel = new JLabel("Enthaltene Textfragmente (mehrere mit Komma trennen):");
        
        containingStrings.addKeyListener(this);
        
        
        
        JLabel unleashStringsLabel = new JLabel("Nicht enthaltene Textfragmente (mehrere mit Komma trennen):");
        unleashStrings.addKeyListener(this);
        
        
        JButton addButton;
        if (change)
            addButton = new JButton("Ändern");
        else
            addButton = new JButton("Hinzufügen");
        
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ETypeCollection eTypeCollection = MainSettingsManager.getSetting().geteTypeCollection();
                ArrayList<String> split_contains = new ArrayList<String>(Arrays.asList(containingStrings.getText().split(",")));
                split_contains.remove("");
                
                for (String split_comp : split_contains)
                {
                    split_comp.trim();
                }
                
                ArrayList<String> split_unleash = new ArrayList<String>(Arrays.asList(unleashStrings.getText().split(",")));
                split_unleash.remove("");
                
                for (String split_comp : split_unleash)
                {
                    split_comp.trim();
                }
                
                
                ArrayList<AusrueckOrdnung> aROs = MainSettingsManager.getSetting().getAusrueckordnungCollection().getAROs();
                
                aro = new AusrueckOrdnung(aROs.size(), nameField.getText(), eTypeCollection.getEType(etypes.getSelectedIndex()), 
                        split_contains.toArray(new String[split_contains.size()]), 
                        split_unleash.toArray(new String[split_unleash.size()]));
                
                dispose();
            }
        });
        add(contentPanel);
        
        contentPanel.add(nameLabel);
        contentPanel.add(autoName);
        contentPanel.add(nameField);
        contentPanel.add(etypes_label);
        contentPanel.add(etypes);
        contentPanel.add(containingStringsLabel);
        contentPanel.add(containingStrings);
        contentPanel.add(unleashStringsLabel);
        contentPanel.add(unleashStrings);
        contentPanel.add(addButton);
        setVisible(true);
        
        setAutoName();
    }
    
    
    
    public static AusrueckOrdnung getARO() {
        return aro;
    }

    private void setAutoName()
    {
        if (!autoName.isSelected())
            return;
        else
        {
            String containString = "";
            String unleashString = "";
        
            if (!containingStrings.getText().isEmpty())
                containString = " + [" + containingStrings.getText() + "]";
            if (!unleashStrings.getText().isEmpty())
                unleashString = " - [" + unleashStrings.getText() + "]";
            EType eType = MainSettingsManager.getSetting().geteTypeCollection().getEType(etypes.getSelectedIndex());
            nameField.setText(eType.getName() + containString + unleashString);
        }
        
         
    }
    
    public void keyTyped(KeyEvent e) {
        setAutoName();
    }

    public void keyPressed(KeyEvent e) {}

    public void keyReleased(KeyEvent e) {}

    
}
